﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            tb_username.Focus();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string code = MasApp.BLL.SessUtil.getValidateKey(Session);
        if (!code.Equals(tb_vali_code.Text) || Label1.Visible == true)
        {
            MasApp.BLL.ResponseHelper.alert("验证码错误 或者 用户名已使用！");
        }
        else
        {
            #region 取得页面回传信息（暂不做处理）
            string log_username = tb_username.Text;
            string log_password = tb_password.Text;
            string org_name = tb_organization.Text;
            string org_address = tb_organizationaddress.Text;
            string org_postcode = tb_org_postcode.Text;
            string org_phone = tb_phone.Text;
            string org_cp = tb_contactperson.Text;
            string org_ce = tb_contactemail.Text;
            string org_d = tb_description.Text;
            string us_username = tb_username.Text;
            string us_realname = tb_realname.Text;
            #endregion

            #region REGorganization _org
            MasApp.Model.organization org = new MasApp.Model.organization();
            org.Creator = tb_username.Text;
            org.Organizationname = tb_organization.Text;
            org.Address = tb_organizationaddress.Text;
            org.Postcode = tb_org_postcode.Text;
            org.Phone = tb_phone.Text;
            org.Contactperson = tb_contactperson.Text;
            org.Contactemail = tb_contactemail.Text;
            org.Description = tb_description.Text;

            int _org = MasApp.BLL.reg.RegOrganization(org);
            #endregion REGorganization _org

            #region REGlogin _log
            MasApp.Model.login log = new MasApp.Model.login();
            log.Username = tb_username.Text;
            log.Password = tb_password.Text;

            int _log = MasApp.BLL.reg.RegLogin(log);
            #endregion REGlogin _log

            #region REGusers _us
            MasApp.Model.users us = new MasApp.Model.users();
            us.Username = tb_username.Text;
            us.Realname = tb_realname.Text;
            us.Sex = ddl_sex.SelectedValue;
            us.Email = tb_email.Text;
            us.Mphone = tb_mphone.Text;
            us.Phone = tb_phone.Text;
            us.Address = tb_address.Text;
            us.Postcode = tb_postcode.Text;
            us.Organizationid = MasApp.DAL.Search.SearchOrgidByCreator(tb_username.Text);
            us.Description = "生日：" + Request["birthday"].ToString();

            int _us = MasApp.BLL.reg.RegUsers(us);
            #endregion REGusers _us

            #region REG_userrole
            MasApp.Model.userrole ur = new MasApp.Model.userrole();
            ur.Userid = MasApp.BLL.Search.SearchUserID(tb_username.Text);
            ur.Roleid = 3;
            int _ur = MasApp.BLL.reg.RegUserRole(ur);
            #endregion


            #region 后端处理
            if (_org > 0 && _log > 0 && _us > 0 && _ur > 0)
            {
                MasApp.BLL.ResponseHelper.alert("恭喜你注册成功！请保管好您的账号密码，现在可以登录了！", "Default.aspx");
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("对不起，由于系统繁忙，您的注册资料并没有完全注册成功，请记住错误代号，并联系我们的管理员，感谢您对我们的支持！错误代号：" + _org + _log + _us + _ur);
            }
            #endregion
        }
    }
    protected void tb_username_TextChanged(object sender, EventArgs e)
    {
        if (MasApp.BLL.Search.HaveUsername(tb_username.Text))
        {
            Label1.Visible = true;
            tb_username.Focus();
        }
        else
        { Label1.Visible = false; }
    }
}
